import 'dart:io';

import 'package:amap_location_fluttify/amap_location_fluttify.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_shop/jpush/jpush_service.dart';
import 'package:flutter_shop/pages/base/base_presenter.dart';
import 'package:flutter_shop/pages/base/base_state.dart';
import 'package:flutter_shop/pages/common/splash_page/splash_page.dart';
import 'package:flutter_shop/provider/providers.dart';
import 'package:flutter_shop/res/colors.dart';
import 'package:flutter_shop/routers/routers.dart';
import 'package:flutter_shop/utils/log_utils.dart';
import 'package:flutter_shop/utils/utils.dart';
import 'package:fluwx/fluwx.dart';
import 'package:oktoast/oktoast.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(MyApp());
  // 透明状态栏
  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    init();
  }

  void init() async {
    //初始化路由
    final router = Router();
    Routers.configureRoutes(router);

    //初始化Log
    Log.init();

    //初始化高德地图
    //是否开启日志
    await enableFluttifyLog(!Utils.isProd);
    //初始化高德地图IOSkey,安卓的key到AndroidManifest里设置
    await AmapCore.init('c21b3fcd3672ff5be714ac86b2536ff5');

    //初始化极光推送,注意AndroidManifest和app下的build.gradle里manifestPlaceholders配置key
    JpushService().init("af10e50936fb01f5b5340234");

    //初始化微信SDK
    await registerWxApi(
      appId: "wxb4ba3c02aa476ea1",
      doOnAndroid: true,
      doOnIOS: false,
      universalLink: "https://your.univerallink.com/link/",
    );
    var result = await isWeChatInstalled;
    print("is installed $result");

    // 设置EasyRefresh的默认样式
    EasyRefresh.defaultHeader = ClassicalHeader(
      completeDuration: Duration(milliseconds: 0),
      enableInfiniteRefresh: false,
//      refreshedText: "刷新完成",
//      refreshText: "拉动刷新",
//      infoText: "更新于 %T",
//      refreshingText: "正在刷新...",
//      refreshReadyText: "释放刷新",
//      refreshFailedText: "刷新失败",
//      noMoreText: "没有更多数据",
      infoColor: CommonColors.gray,
    );
    EasyRefresh.defaultFooter = ClassicalFooter(
      enableInfiniteLoad: true,
//      noMoreText: "没有更多数据",
//      loadFailedText: "加载失败",
//      loadingText: "正在加载...",
//      infoText: "更新于 %T",
//      loadedText: "加载完成",
//      loadReadyText: "释放加载",
//      loadText: "拉动加载",
      infoColor: CommonColors.gray,
    );
  }

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: providers,
      child: OKToast(
        child: MaterialApp(
          title: "百姓生活+",
          onGenerateRoute: Routers.router.generator,
          theme: ThemeData(
            platform: TargetPlatform.android,
            primarySwatch: CommonColors.primarySwatch,
            scaffoldBackgroundColor: CommonColors.common_bg,
            cursorColor: CommonColors.primary,
          ),
          home: SplashPage(),
          localizationsDelegates: [
            GlobalEasyRefreshLocalizations.delegate,
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
          ],
          supportedLocales: [
            const Locale('zh', 'CN'),
            const Locale('zh', 'CH'),
            const Locale('en', 'US'),
          ],
        ),
      ),
    );
  }
}
